package com.topone.controller;

import com.topone.Exception.FanZoneException;
import com.topone.Utils.ResultVOUntil;
import com.topone.VO.ResultVO;
import com.topone.dto.BussinessDTO;
import com.topone.dto.OrderDTO;
import com.topone.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/12/15,0015.
 */
@CrossOrigin
@RestController
@RequestMapping("/seller/order")
@Slf4j
public class SellerOrderController {

    @Autowired
    private OrderService orderService;

    @GetMapping("/list")
    public ResultVO<Map<String,Object>> list(@RequestParam(value = "orderStatus", required = false) Integer orderStatus,
                                             @RequestParam(value = "bussinessName",required = false) String bussinessName,
                                             @RequestParam(value = "bussinessId",required = false) String bussinessId) {
        Map<String, Object> map = new HashMap<>();
        List<OrderDTO> orderDTOList;
        BussinessDTO bussinessDTO = new BussinessDTO();
        Map<String,Object> result=new HashMap<>();
        try {
            if (orderStatus != null) {
                orderDTOList = orderService.findList(orderStatus);
                map.put("orderDTOList", orderDTOList);
                log.info("卖家端查询所有订单成功:{}", orderDTOList);
                return ResultVOUntil.success(map);
            } else if (StringUtils.isNotEmpty(bussinessId)) {
                bussinessDTO.setBussinessId(bussinessId);
                 result= orderService.findOrdersByBusiness(bussinessDTO);
            } else if (StringUtils.isNotEmpty(bussinessName)) {
                bussinessDTO.setBussinessName(bussinessName);
                 result = orderService.findOrdersByBusiness(bussinessDTO);
            }
        } catch (Exception e) {
            log.error("查询订单信息失败:{}", e.fillInStackTrace());
            return ResultVOUntil.error(1, e.fillInStackTrace().toString());
        }
        map.put("result", result);
        log.info("卖家端查询所有订单成功:{}",map);
        return ResultVOUntil.success(map);
    }

    @GetMapping("/cancel")
    public ResultVO cancel(@RequestParam("orderId") String orderId,
                           Map<String, Object> map) {
        try {
            OrderDTO orderDTO = orderService.findOne(orderId);
            orderService.cancel(orderDTO);
        } catch (FanZoneException e) {
            log.error("【卖家取消订单】 发生异常", e.getMessage());
            return ResultVOUntil.error(1, "【卖家取消订单】 发生异常" + e.getMessage());
        }
        log.info("【卖家取消订单】 取消成功 orderId={}", orderId);
        map.put("success", "【卖家取消订单】 取消成功 orderId=" + orderId);
        return ResultVOUntil.success(map);

    }

    /**
     * 卖家端查询订单详情
     *
     * @param orderId
     * @param map
     * @return
     */
    @GetMapping("/detail")
    public ResultVO detail(@RequestParam("orderId") String orderId,
                           Map<String, Object> map) {

        OrderDTO orderDTO = new OrderDTO();
        try {
            orderDTO = orderService.findOne(orderId);
            map.put("orderDTO", orderDTO);
        } catch (FanZoneException e) {
            log.error("【卖家查询订单详情】查询异常", e);
            map.put("msg", e.getMessage());
            map.put("url", "/fanZone/seller/order/list");
            return ResultVOUntil.error(1, "【卖家查询订单详情】查询异常:" + e.getMessage());

        }
        log.info("【卖家查询订单详情】成功:{}", map);
        return ResultVOUntil.success(map);
    }

    /**
     * 完结订单
     *
     * @param orderId
     * @param map
     * @return
     */
    @GetMapping("/finish")
    public ResultVO finish(@RequestParam("orderId") String orderId,
                           Map<String, Object> map) {

        OrderDTO orderDTO = new OrderDTO();
        try {
            orderDTO = orderService.findOne(orderId);
            orderService.finish(orderDTO);
        } catch (FanZoneException e) {
            log.error("【卖家完结订单】 发生异常{}", e.getMessage());
            return ResultVOUntil.error(1, "【卖家完结订单】 发生异常" + e.getMessage());
        }
        log.info("【卖家完结订单】成功 orderId={}", orderId);
        map.put("success", "【卖家完结订单】成功 ");
        return ResultVOUntil.success(map);

    }


}
